package com.sunday.volunteer.ui.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.adapter.GroupAdapter;
import com.sunday.volunteer.entity.Member;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.utils.BaseUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/1/12.
 */

public class GroupActivity extends BaseActivity{

    @Bind(R.id.title_text)
    TextView title_text;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.swipfresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    private int pageNo = 1;
    private boolean isCanloadMore = false;
    private LinearLayoutManager layoutManager;
    private GroupAdapter adapter;
    private List<Member> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        ButterKnife.bind(this);

        title_text.setText("我的团队");

        list = new ArrayList<>();
        adapter = new GroupAdapter(list ,R.layout.item_group);
        adapter.setOnClickListener(onClickListener);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        swipeRefreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .drawable(R.drawable.shape_divider)
                .build());
        recyclerView.setAdapter(adapter);


        handlerRecyclerView();
        getData();
    }

    @OnClick(R.id.add_group)
    void addGroup(){
        BaseUtil.openActivity(mContext , AddGroupActivity.class);
    }

    private int lastVisibleItem;

    private void handlerRecyclerView() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                getData();
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount() && isCanloadMore) {
                    pageNo ++;
                    getData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                isCanloadMore = dy >0;
            }
        });
    }


    private void getData() {
        Call<ResultDO<List<Member>>> call = HttpClient.getHttpAdapter().getGruops( BaseApp.getInstance().getMemberId(),pageNo ,10 , null ,null);
        call.enqueue(new Callback<ResultDO<List<Member>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Member>>> call, Response<ResultDO<List<Member>>> response) {
                if(response.body() == null || isFinish){
                    return;
                }
                if(response.body().getCode() == 0 && response.body().getResult() != null){
                    if(pageNo == 1){
                        list.clear();
                    }
                    list.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                }
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(Call<ResultDO<List<Member>>> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.item:
                    Member item = (Member) v.getTag(R.id.item);
                    if(item == null){
                        return;
                    }

                    BaseUtil.openGroupDetialActivity(mContext , item.getId() );
                    break;
            }

        }
    };

}
